package com.chen.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chen.domain.CoinType;
import com.chen.dto.R;
import com.chen.service.CoinTypeService;
import io.swagger.annotations.Api;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: chen
 * @Date: 2021/5/11 9:55 AM
 * @Version 1.0
 */
@RestController
@Api(tags = "币种类型控制器")
@RequestMapping("/coinTypes")
public class CoinTypeController {


    @Autowired
    private CoinTypeService coinTypeService;

    @GetMapping
    public R<Page<CoinType>> list(String code,Page<CoinType> page){
        QueryWrapper<CoinType> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(code),"code",code);
        queryWrapper.orderByDesc("created");
        Page<CoinType> list = coinTypeService.page(page, queryWrapper);
        return R.ok(list);
    }

    @PostMapping
    public R add(@RequestBody CoinType coinType){
        coinTypeService.save(coinType);
        return R.ok();
    }

    @PatchMapping
    public R update(@RequestBody CoinType coinType){
        coinTypeService.updateById(coinType);
        return R.ok();
    }

    @PostMapping("/setStatus")
    public R setStatus(@RequestBody CoinType coinType){
        coinTypeService.updateById(coinType);
        return R.ok();
    }

    @GetMapping("/all")
    public R<List<CoinType>> all(String status){
        QueryWrapper<CoinType> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(status),"status",status);
        queryWrapper.orderByDesc("created");
        List<CoinType> list = coinTypeService.list(queryWrapper);
        return R.ok(list);
    }

}
